<template>
  <div class="app-container">
    <div class="custom_card_0" v-show="showSearch">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="100px"
      >
        <el-form-item label="优惠券类别" prop="couponCategory">
          <el-select
            v-model="queryParams.couponCategory"
            placeholder="请选择优惠券类别"
            clearable
          >
            <template v-for="dict in dict.type.coupon_type">
              <el-option
                :label="dict.label"
                :value="dict.value"
                :key="dict.value"
              ></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="优惠券名称" prop="couponName">
          <el-input
            v-model="queryParams.couponName"
            placeholder="请输入优惠券名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
<!--        <el-form-item label="优惠券状态" prop="isAvailable">
          <el-select
            v-model="queryParams.isAvailable"
            placeholder="请选择优惠券状态"
            clearable
          >
            <template v-for="dict in dict.type.sys_available">
              <el-option
                :label="dict.label"
                :value="dict.value"
                :key="dict.value"
              ></el-option>
            </template>
          </el-select>
        </el-form-item>-->
<!--        <el-form-item label="领取方式" prop="receiveMethod">
          <el-select
            v-model="queryParams.receiveMethod"
            placeholder="请选择优惠券领取方式"
            clearable
          >
            <el-option label="无限制" value="0"></el-option>
            <el-option label="仅通过活动专区领取" value="1"></el-option>
            <el-option label="仅通过小程序优惠券专区领取" value="2"></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="custom_card">
      <el-row :gutter="10" class="mb10">
        <el-col :span="1.5">
          <el-button
            class="first-plain-btn" size="medium"
            icon="el-icon-plus"
            @click="handleAdd"
            v-hasPermi="['marketingManagement:couponManage:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            class="sec-plain-btn" size="medium"
            icon="el-icon-download"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['marketingManagement:couponManage:export']"
            >导出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="couponManageList"
        @selection-change="handleSelectionChange"
        border
      >
        <el-table-column label="优惠券名称" align="center" prop="couponName" />
        <el-table-column
          label="优惠券类别"
          align="center"
          prop="couponCategory"
          min-width="100"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.coupon_type"
              :value="scope.row.couponCategory"
            />
          </template>
        </el-table-column>
<!--        <el-table-column
          label="领取方式"
          align="center"
          prop="receiveMethod"
          min-width="120"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            {{ scope.row.receiveMethod | receiveMethod }}
          </template>
        </el-table-column>-->
        <el-table-column
          label="库存"
          align="center"
          prop="releaseNum"
          min-width="160"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span>发布：{{ scope.row.releaseNum }}张、</span>
            <span>剩余： {{ scope.row.remainingNumber }}张、</span>
            <span>已领取：{{ scope.row.haveReceive }}张、</span>
            <span>已使用：{{ scope.row.hasBeenUsed }}张 </span>
          </template>
        </el-table-column>
        <!-- <el-table-column
          label="状态"
          align="center"
          prop="isAvailable"
          min-width="80"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_available"
              :value="scope.row.isAvailable"
            />
          </template>
        </el-table-column> -->
        <el-table-column
          label="创建时间"
          align="center"
          prop="create_time"
          min-width="160"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}')
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="创建人"
          align="center"
          prop="userName"
          min-width="120"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          fixed="left"
          min-width="140"
        >
          <template slot-scope="scope">
            <!-- <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-if="scope.row.isAvailable == 0"
              v-hasPermi="['marketingManagement:couponManage:edit']"
              >修改</el-button
            > -->
            <el-button size="mini" type="text" @click="handleDetails(scope.row)"
              >详情</el-button>
            <el-button size="mini" type="text" v-if="scope.row.remainingNumber>0" @click="handleSendUser(scope.row)"
            >发送指定用户</el-button>
<!--            <el-button size="mini" type="text" v-if="scope.row.remainingNumber>0" @click="handleqrcode(scope.row)"
            >二维码</el-button>-->
            <template v-if="scope.row.isAvailable == 0">
              <el-button
                size="mini"
                type="text"
                @click="handleAvailable(scope.row, 1)"
                v-hasPermi="['marketingManagement:couponManage:edit']"
              >启用</el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleDelete(scope.row)"
                v-hasPermi="['marketingManagement:couponManage:delete']"
              >删除</el-button>
            </template>

            <el-button
              size="mini"
              type="text"
              @click="handleAvailable(scope.row, 0)"
              v-if="scope.row.isAvailable == 1"
              v-hasPermi="['marketingManagement:couponManage:edit']"
              >停用</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改优惠券对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="优惠券类别">
              <dict-tag
                :options="dict.type.coupon_type"
                :value="form.couponCategory"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="优惠券名称" prop="couponName">
              <el-input
                v-model="form.couponName"
                placeholder="请输入优惠券名称"
                :maxlength="10"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.couponCategory == 1">
            <el-form-item label="满减要求" prop="conditionMoney">
              <el-input
                v-model="form.conditionMoney"
                placeholder="请输入满减要求"
                :maxlength="10"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col
            :span="12"
            v-if="form.couponCategory == 1 || form.couponCategory == 3"
          >
            <el-form-item label="优惠金额" prop="preferentialMoney">
              <el-input
                v-model="form.preferentialMoney"
                placeholder="请输入优惠金额"
                :maxlength="10"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.couponCategory == 2">
            <el-form-item label="折扣" prop="discount">
              <el-input
                v-model="form.discount"
                placeholder="请输入折扣(1-10)"
                :maxlength="3"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="过期天数" prop="expiredDay">
              <el-input
                v-model="form.expiredDay"
                placeholder="请输入过期天数"
                :maxlength="10"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布数量" prop="releaseNum">
              <el-input
                v-model="form.releaseNum"
                placeholder="请输入发布数量"
                :maxlength="10"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="业务类型" prop="bizType">
              <el-radio-group v-model="form.bizType" @change="bizTypeChange">
                <el-radio border :label="1">不限制</el-radio>
                <el-radio border :label="2">指定业务</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.bizType == 2">
            <el-form-item label="业务分类" prop="bizClassification">
              <el-radio-group
                v-model="form.bizClassification"
                @change="bizClassificationChange"
              >
                <el-radio
                  border
                  v-for="dict in tenantBusiness"
                  :key="dict.busId"
                  :label="parseInt(dict.busId)"
                  >{{ dict.busName }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="车型限制" prop="specifyModel">
              <el-radio-group v-model="form.specifyModel">
                <el-radio border :label="1">不限制</el-radio>
                <el-radio border :label="2" v-if="form.bizType == 2"
                  >指定车型</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.specifyModel == 2">
            <el-form-item label="指定车型" prop="modelName">
              <el-input
                v-model="form.modelInfo"
                placeholder="请选择指定车型"
                readonly
                :style="{ width: '50%' }"
              >
                <template slot="append"
                  ><span @click="queryModel">选择车型</span></template
                >
              </el-input>
            </el-form-item>
          </el-col>
<!--          <el-col :span="24">
            <el-form-item label="领取方式" prop="receiveMethod">
              <el-checkbox-group v-model="form.receiveMethod" size="medium">
                <el-checkbox border label="1">仅通过活动专区领取</el-checkbox>
                <el-checkbox border label="2"
                  >仅通过小程序优惠券专区领取</el-checkbox
                >
              </el-checkbox-group>
            </el-form-item>
          </el-col>-->
          <el-col :span="24">
            <el-form-item label="补充说明" prop="supplementExplain">
              <el-input
                v-model="form.supplementExplain"
                type="textarea"
                :maxlength="100"
                show-word-limit
                placeholder="请输入补充说明"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="创建优惠券"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="cancelDialogVisible"
    >
      <el-form ref="form1" :model="form1" :rules="rules" label-width="100px">
        <el-form-item label="优惠券类别" prop="couponCategory">
          <el-radio-group v-model="form1.couponCategory" size="medium">
            <el-radio
              v-for="dict in dict.type.coupon_type"
              :label="dict.value"
              :value="dict.value"
              :key="dict.value"
              >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelDialogVisible">取 消</el-button>
        <el-button type="primary" @click="addCouponManage">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 详情 -->
    <el-dialog
      :title="title"
      :visible.sync="openDetails"
      append-to-body
      width="600px"
    >
      <el-descriptions
        :column="2"
        border
        :labelStyle="{ width: '120px' }"
        :contentStyle="{ width: '200px' }"
      >
        <el-descriptions-item label-class-name="my-descriptions-label" >
          <template slot="label"> 优惠券类别</template>
          <dict-tag
            :options="dict.type.coupon_type"
            :value="form.couponCategory"
          />
        </el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label" >
          <template slot="label"> 优惠券名称</template>
          {{ form.couponName }}
        </el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label"  v-if="form.couponCategory == 1">
          <template slot="label"> 满减要求</template>
          {{ form.conditionMoney }}元
        </el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label"
          v-if="form.couponCategory == 1 || form.couponCategory == 3"
        >
          <template slot="label"> 优惠金额</template>
          {{ form.preferentialMoney }}元
        </el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label"  v-if="form.couponCategory == 2">
          <template slot="label"> 折扣</template>
          {{ form.discount }}
        </el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label" >
          <template slot="label"> 过期天数</template>
          {{ form.expiredDay }}天
        </el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label" >
          <template slot="label"> 发布数量</template>
          {{ form.releaseNum }}张
        </el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label" >
          <template slot="label"> 剩余数量</template>
          {{ form.remainingNumber }}张
        </el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label" >
          <template slot="label"> 业务类型</template>
          {{ form.bizType == 1 ? '不限制' : '指定业务' }}
        </el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label"  v-if="form.bizType == 2">
          <template slot="label"> 业务分类</template>
          <el-radio-group v-model="form.bizClassification">
            <el-radio
              border
              v-if="form.bizClassification == dict.busId"
              disabled
              v-for="dict in tenantBusiness"
              :key="dict.busId"
              :label="parseInt(dict.busId)"
              >{{ dict.busName }}</el-radio
            >
          </el-radio-group>
        </el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label" >
          <template slot="label"> 车型限制</template>
          {{ form.specifyModel == 1 ? '不限制' : '指定车型' }}
        </el-descriptions-item>
        <el-descriptions-item label-class-name="my-descriptions-label"  v-if="form.specifyModel == 2">
          <template slot="label"> 指定车型</template>
          {{ form.modelName }}
        </el-descriptions-item>
<!--        <el-descriptions-item label-class-name="my-descriptions-label"  span="2">
          <template slot="label"> 领取方式</template>
          <el-checkbox-group v-model="receiveMethodOptions" size="medium">
            <el-checkbox label="1" disabled>仅通过活动专区领取</el-checkbox>
            <el-checkbox label="2" disabled
              >仅通过小程序优惠券专区领取</el-checkbox
            >
          </el-checkbox-group>
        </el-descriptions-item>-->
        <el-descriptions-item label-class-name="my-descriptions-label" >
          <template slot="label"> 补充说明</template>
          {{ form.supplementExplain }}
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDetails">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="优惠券二维码"
      :visible.sync="openqrcode"
      width="500px"
      destroy-on-close
      append-to-body
      @close="cancelqrcode"
    >
      <div style="text-align: center">
        <el-image class="preview" :src="getImgUrl(url)" fit="contain">
        </el-image>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-link
          type="primary"
          :underline="false"
          style="font-size: 12px; vertical-align: baseline"
          @click="importQrCord"
          >下载二维码</el-link
        >
      </div>
    </el-dialog>
    <!-- 车型方案列表 -->
    <el-drawer
      title="车型方案"
      :visible.sync="modelPlanFlag"
      :direction="direction"
      size="50%"
      :before-close="cancelModelPlan"
    >
      <el-form
        :model="queryParamsModelPlan"
        ref="queryFormModelPlan"
        :inline="true"
        label-width="80px"
      >
        <!--		     <el-form-item label="方案名称" prop="planName">-->
        <!--		         <el-input v-model="queryParamsModelPlan.planName" placeholder="请输入方案名称" clearable size="small" @keyup.enter.native="handleQueryModelPlan" />-->
        <!--		     </el-form-item>-->
        <el-form-item label="车型名称" prop="modelName">
          <el-input
            v-model="queryParamsModelPlan.modelName"
            placeholder="请输入车型名称"
            clearable
            size="small"
            @keyup.enter.native="handleQueryModelPlan"
          />
        </el-form-item>
        <el-form-item label="所属门店" prop="deptId">
          <el-select
            v-model="queryParamsModelPlan.deptId"
            placeholder="请选择所属门店"
            clearable
            size="small"
            filterable
            remote
          >
            <el-option
              v-for="(item, i) in storeList"
              :key="item.deptId"
              :label="item.storeName"
              :value="item.deptId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQueryModelPlan"
            >搜索</el-button
          >
          <el-button
            icon="el-icon-refresh"
            size="mini"
            @click="resetQueryModelPlan"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table v-loading="loadingmodelPlan" :data="modelList">
        <el-table-column label="品牌车系" align="center" prop="modelInfo" />
        <el-table-column label="车型名称" align="center" prop="modelName" />
        <el-table-column label="所属门店" align="center" prop="deptName" />
        <el-table-column label="所属租户" align="center" prop="tenantName" />
        <el-table-column
          label="操作"
          align="left"
          class-name="small-padding fixed-width"
          fixed="right"
          width="180"
        >
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="selectPlan(scope.row)"
              >选择</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="totalPlan > 0"
        :total="totalPlan"
        :page.sync="queryParamsModelPlan.pageNum"
        :limit.sync="queryParamsModelPlan.pageSize"
        @pagination="getModelPlan"
      />
    </el-drawer>
    <!-- 客户发送优惠券 -->
    <el-dialog :title="sendUser.title" :visible.sync="sendUser.open"  append-to-body>
      <el-form
        :model="sendUser.queryParams"
        ref="sendUserQueryForm"
        :inline="true"
        label-width="80px"
        v-show="showSearch"
      >
        <el-form-item label="客户名称" prop="memberName">
          <el-input
            v-model="sendUser.queryParams.memberName"
            placeholder="请输入客户名称"
            clearable
            size="small"
            @keyup.enter.native="searchHandleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="searchHandleQuery"
          >查询</el-button
          >
          <el-button
            icon="el-icon-refresh"
            size="mini"
            @click="sendUserResetQuery"
          >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        v-loading="sendUser.loading"
        :data="sendUser.list"
        @selection-change="sendUserSelectionChange"
        height="500px"
      >
        <el-table-column type="selection" width="55"  align="center" />
        <el-table-column
          min-width="100"
          label="客户名称"
          align="center"
          prop="memberName"
        />
        <el-table-column
          min-width="140"
          label="客户联系方式"
          align="center"
          prop="contactPhone"
        />
      </el-table>
      <pagination
        :total="sendUser.total"
        :page.sync="sendUser.queryParams.pageNum"
        :limit.sync="sendUser.queryParams.pageSize"
        @pagination="sendUserHandleQuery"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sendUsersubmitForm">确 定</el-button>
        <el-button @click="sendUsercancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCouponManage,
  downloadImgs,
  getCouponManage,
  delCouponManage,
  addCouponManage,
  updateCouponManage,
  exportCouponManage,
  stopUsingEnabled,
  insertcouponMember
} from '@/api/marketingManagement/couponManage'
import {
  listBasic
} from '@/api/member/member'
import {
  getCarBrandList,
  getCarModelList,
  getCarSeriesList,
  getStoreList,
  duanlistPlan,
  changlistPlan,
  haolistPlan,
} from '@/api/car/model'
import { tenantBusiness } from '@/api/car/business'

export default {
  name: 'CouponManage',
  dicts: ['coupon_type', 'sys_available'],
  data() {
    return {
      // 查询参数--车型方案
      queryParamsModelPlan: {
        pageNum: 1,
        pageSize: 10,
        planName: null,
        modelName: null,
      },
      // 遮罩层
      loading: true,
      direction: 'rtl',
      loadingmodelPlan: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠券表格数据
      couponManageList: [],
      tenantBusiness: [],
      modelList: [],
      modelPlanFlag: false,
      totalPlan: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openDetails: false,
      openqrcode: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        couponCategory: null,
        receiveMethod: null,
        couponName: null,
        isAvailable: null,
      },
      // 车辆列表表格数据
      storeList: [],
      // 表单参数
      form: {
        couponCategory: '1',
      },
      form1: { couponCategory: '1' },
      url: null,
      dialogVisible: false,
      // 表单校验
      rules: {
        couponCategory: [{ required: true, message: '请选择创建类型！' }],
        couponName: [{ required: true, message: '优惠券名称不能为空！' }],
        conditionMoney: [
          { required: true, message: '满减要求不能为空！', trigger: 'change' },
          {
            pattern: /^(([1-9][0-9]*)|(([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2})))$/,
            message: '满减要求输入有误',
            trigger: 'blur',
          },
        ],
        preferentialMoney: [
          {
            required: true,
            message: '优惠券金额不能为空！',
            trigger: 'change',
          },
          {
            pattern: /^(([1-9][0-9]*)|(([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2})))$/,
            message: '优惠券金额输入有误',
            trigger: 'blur',
          },
        ],
        discount: [
          { required: true, message: '折扣范围不能为空！', trigger: 'change' },
          {
            pattern: /^(10|[1-9]|([1-9]\.\d{0,2}))$/,
            message: '折扣范围输入有误',
            trigger: 'blur',
          },
        ],
        expiredDay: [
          { required: true, message: '过期天数不能为空！', trigger: 'change' },
          {
            pattern: /^([1-9]\d*)$/,
            message: '值为正整数',
            trigger: 'blur',
          },
        ],
        releaseNum: [
          { required: true, message: '发布数量不能为空！', trigger: 'change' },
          {
            pattern: /^([1-9]\d*)$/,
            message: '值为正整数',
            trigger: 'blur',
          },
        ],
        upperLimit: [
          {
            required: true,
            message: '领取上限/人不能为空！',
            trigger: 'change',
          },
          {
            pattern: /^([1-9]\d*)$/,
            message: '值为正整数',
            trigger: 'blur',
          },
        ],
        specifyModel: [{ required: true, message: '指定车型不能为空！' }],
        modelName: [{ required: true, message: '请选择车型！' }],
        receiveMethod: [
          { required: true, message: '领取方式不能为空！', trigger: 'change' },
        ],
        bizType: [{ required: true, message: '业务类型不能为空！' }],
        bizClassification: [{ required: true, message: '业务分类不能为空！' }],
      },
      receiveMethodOptions: [],
      sendUser: {
        open: false,
        title: '发送指定客户',
        list: [],
        total: 0,
        loading:false,
        remainingNumber:0,
        sendList: {
          memberVoList: [],
          businessId: '',
          couponId: '',
          couponName: '',
        },
        queryParams: {
          memberName: '',
          pageNum: 1,
          pageSize: 10,
          distributedNodeId:null
        },
      },
    }
  },
  filters: {
    receiveMethod(val) {
      console.log(val)
      return (
        {
          1: '仅通过活动专区领取',
          2: '仅通过小程序优惠券专区领取',
        }[Number(val)] || '无限制'
      )
    },
  },
  created() {
    this.getList()
    this.tenantBusinessList()
    getStoreList().then((response) => {
      this.storeList = response.data
    })
  },
  methods: {
    queryModel() {
      if (this.form.bizClassification == null) {
        alert('请选择业务类型')
        return
      }
      this.getModelPlan()
    },
    getModelPlan() {
      this.modelPlanFlag = true
      if (this.form.bizClassification == 1) {
        //日租
        this.loadingmodelPlan = true
        duanlistPlan(this.queryParamsModelPlan).then((response) => {
          this.modelList = response.rows
          this.totalPlan = response.total
          this.loadingmodelPlan = false
        })
      }
      if (this.form.bizClassification == 2) {
        //月租
        changlistPlan(this.queryParamsModelPlan).then((response) => {
          this.modelList = response.rows
          this.totalPlan = response.total
          this.loadingmodelPlan = false
        })
      }
      if (this.form.bizClassification == 3) {
        //豪车
        haolistPlan(this.queryParamsModelPlan).then((response) => {
          this.modelList = response.rows
          this.totalPlan = response.total
          this.loadingmodelPlan = false
        })
      }
    },
    resetQueryModelPlan() {
      this.resetForm('queryFormModelPlan')
      this.handleQueryModelPlan()
    },
    handleQueryModelPlan() {
      this.getModelPlan()
    },
    bizTypeChange() {
      if (this.form.bizType == 1) {
        this.form.specifyModel = 1
        this.form.bizClassification = null
        this.form.modelId = null
        this.form.modelName = null
        this.form.modelInfo = null
        this.form.planId = null
      }
    },
    bizClassificationChange() {
      this.form.modelId = null
      this.form.modelName = null
      this.form.modelInfo = null
      this.form.planId = null
    },
    selectPlan(row) {
      this.resetForm('queryFormModelPlan')
      this.modelPlanFlag = false
      this.form.modelId = row.modelId
      console.log(row.modelName)
      this.form.modelName = row.modelName
      this.form.modelInfo = row.modelInfo + ' ' + row.modelName
      this.form.planId = row.planId
    },
    /* 二维码*/
    handleqrcode(row) {
      ;(this.openqrcode = true), (this.url = row.qrCode)
    },
    //下载二维码
    importQrCord() {
      const url = this.getImgUrl(this.url) //图片的https链接
      const imgName = url.substr(url.lastIndexOf('/') + 1)
      downloadImgs({
        url: url,
      }).then((res) => {
        let href = window.URL.createObjectURL(new Blob([res])) // 根据后端返回的url对应的文件流创建URL对象
        const link = document.createElement('a') //创建一个隐藏的a标签
        link.style.display = 'none'
        link.href = href //设置下载的url
        link.download = imgName //设置下载的文件名
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        window.URL.revokeObjectURL(href) // 释放掉blob对象
      })
    },

    cancelqrcode() {
      this.openqrcode = false
    },
    /** 启用/停用 */
    handleAvailable(row, available) {
      stopUsingEnabled({
        couponId: row.couponId,
        isAvailable: available,
      }).then((response) => {
        this.$modal.msgSuccess('修改成功')
        this.open = false
        this.getList()
      })
    },
    tenantBusinessList() {
      tenantBusiness().then((response) => {
        this.tenantBusiness = response.data
      })
    },
    /** 查询优惠券列表 */
    getList() {
      this.loading = true
      listCouponManage(this.queryParams).then((response) => {
        this.couponManageList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
      this.resetform1()
    },
    cancelDetails() {
      this.openDetails = false
      this.reset()
      this.resetform1()
    },
    // 表单重置
    reset() {
      this.form = {
        couponCategory: null,
        couponId: null,
        couponName: null,
        conditionMoney: null,
        preferentialMoney: null,
        discount: null,
        expiredDay: null,
        releaseNum: null,
        upperLimit: 1,
        specifyModel: 1,
        modelId: null,
        modelName: null,
        receiveMethod: null,
        bizType: 1,
        supplementExplain: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        tenantId: null,
      }
      this.resetForm('form')
    },
    // 表单重置
    resetform1() {
      this.form1 = {
        couponCategory: 1,
      }
      this.resetForm('form1')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.couponId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm('form1')
      this.dialogVisible = true
      this.receiveMethodOptions = []
    },
    cancelDialogVisible() {
      this.dialogVisible = false
      this.resetform1()
    },
    cancelModelPlan() {
      this.modelPlanFlag = false
    },
    addCouponManage() {
      this.$refs['form1'].validate((valid) => {
        if (valid) {
          var couponCategory = this.form1.couponCategory
          this.dialogVisible = false
          this.reset()
          this.form.couponCategory = couponCategory
          this.form.receiveMethod = []
          this.open = true
          this.title = '添加优惠券'
        }
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const couponId = row.couponId
      getCouponManage(couponId).then((response) => {
        this.form = response.data
        this.form.receiveMethod = this.form.receiveMethod.split(',')
        this.form.bizClassification = parseInt(this.form.bizClassification)
        this.form.modelInfo = this.form.modelName
        this.open = true
        this.title = '修改优惠券'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.receiveMethod = this.form.receiveMethod.join(',')
          if (this.form.couponId != null) {
            updateCouponManage(this.form)
              .then((response) => {
                this.open = false
                this.$modal.msgSuccess('修改成功')
                this.getList()
              })
              .catch(() => {
                this.form.receiveMethod = this.form.receiveMethod.split(',')
              })
          } else {
            addCouponManage(this.form)
              .then((response) => {
                this.open = false
                this.$modal.msgSuccess('新增成功')
                this.getList()
              })
              .catch(() => {
                this.form.receiveMethod = this.form.receiveMethod.split(',')
              })
          }
        }
      })
    },
    /* 详情 */
    handleDetails(row) {
      this.reset()
      const couponId = row.couponId
      getCouponManage(couponId).then((response) => {
        this.form = response.data
        this.openDetails = true
        this.title = '优惠券详情'
        this.receiveMethodOptions = this.form.receiveMethod.split(',')
        this.form.bizClassification = parseInt(this.form.bizClassification)
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const couponId = row.couponId
      this.$modal
        .confirm('是否确认删除优惠券【' + row.couponName + '】的数据项？')
        .then(function () {
          return delCouponManage(couponId)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal
        .confirm('是否确认导出所有优惠券数据项？')
        .then(() => {
          this.exportLoading = true
          return exportCouponManage(queryParams)
        })
        .then((response) => {
          this.$download.name(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
    //发送指定客户
    handleSendUser(row){
      // 发送指定客户
      this.sendUser.open = true
      this.sendUser.sendList.couponName = row.couponName
      this.sendUser.sendList.couponId = row.couponId
      this.sendUser.remainingNumber = row.remainingNumber
      this.sendUserHandleQuery()
    },
    /** 查询客户列表 */
    sendUserHandleQuery() {
      this.sendUser.loading = true
      listBasic(this.sendUser.queryParams).then((response) => {
        this.sendUser.list = response.rows
        this.sendUser.total = response.total
        this.sendUser.loading = false
      })
    },
    sendUsercancel() {
      this.sendUser.open = false
      this.resetForm('sendUserQueryForm')
    },
    searchHandleQuery() {
      this.sendUser.queryParams.pageNum = 1
      this.sendUserHandleQuery()
    },

    /** 重置按钮操作 */
    sendUserResetQuery() {
      this.resetForm('sendUserQueryForm')
      this.sendUserHandleQuery()
    },
    // 选择指定用户数据
    sendUserSelectionChange(selection) {
      this.sendUser.sendList.memberVoList = selection
    },
    // 提交指定用户
    sendUsersubmitForm() {
      if (!this.sendUser.sendList.memberVoList.length) {
        this.$message.error('指定客户不能为空')
        return false
      }
      if(this.sendUser.remainingNumber<this.sendUser.sendList.memberVoList.length){
        this.$message.error('优惠券库存不足')
        return false
      }
      insertcouponMember(this.sendUser.sendList).then((res) => {
        this.$message.success(res.msg)
        this.sendUser.open = false
        this.getList()
      })
    },
  },
}
</script>
